"use strict";
const way = require("../utils/way");
const Result = require("../basics/result");

/**
 * 查询列表
 * @param {*} cloud 
 * @param {*} param1 
 */
exports.selectList = async (cloud,session, { template_id, page_num = 1, page_size = 20 }) => {
  let data = { template_id, page_num, page_size };
  data.session = session;
  console.log("temps params", data);
  const results = await way.taobao(cloud, "taobao.miniapp.template.queryapp", data);
  console.log("temps results", results);
  const res = results.all_version_infos;
  if (!res) {
    throw Result.error(Result.constant.taobaofail, res.msg);
  }
  return res.miniapp_instance_app_all_versions_dto;
};

/**
 * 模板实例化
 * @param {*} cloud 
 * @param {*} param1 
 */
exports.instantiate = async (cloud,session, { clients, description, ext_json, icon, template_id, template_version, name }) => {
  let data = { clients, description, ext_json, icon, template_id, template_version,name, alias:name };
  data.session = session;
  console.log("temp instantiate params", data);
  const results = await way.taobao(cloud, "taobao.miniapp.template.instantiate", data);
  console.log("temp instantiate results", results);
  const error = results.error_response;
  if (error) {
    throw Result.error(Result.constant.taobaofail, error.sub_msg);
  }
  return true;
};

/**
 * 更新实例化
 * @param {*} cloud 
 * @param {*} param1 
 */
exports.updateapp = async (cloud,session, { clients, app_id, ext_json, template_id, template_version }) => {
  let data = { clients, app_id, ext_json, template_id, template_version };
  data.session = session;
  console.log("temp updateapp params", data);
  const results = await way.taobao(cloud, "taobao.miniapp.template.updateapp", data);
  console.log("temp updateapp results", results);
  const error = results.error_response;
  if (error) {
    throw Result.error(Result.constant.taobaofail, error.sub_msg);
  }
  return true;
};

/**
 * 上线实例化应用
 * @param {*} cloud 
 * @param {*} param1 
 */
exports.onlineapp = async (cloud,session, { clients, app_id, app_version, template_id, template_version }) => {
  let data = { clients, app_id, app_version, template_id, template_version };
  data.session = session;
  console.log("temp onlineapp params", data);
  const results = await way.taobao(cloud, "taobao.miniapp.template.onlineapp", data);
  console.log("temp onlineapp results", results);
  const error = results.error_response;
  if (error) {
    throw Result.error(Result.constant.taobaofail, error.sub_msg);
  }
  return true;
};

